<?php
class LoginAction extends CommonAction {

    public function index() {
        $this->display();
    }

    public function login() {
        $admin = D('admin');
        if($this->_post('checkCode') !== session('code')) $this->error('验证码错误!', 0);
        $name = $this->_post('name');
        $pass = sha1($this->_post('pass'));
        if(empty($name) || empty($pass)) $this->error('管理员名称和密码必须填写!', 0);
        if(!$admin->where("name='$name'")->getField('id')) {
            $this->error('管理员不存在!', 0);
        } else {
            if(!$admin->where("pass='$pass'")->getField('id')) $this->error('管理员密码错误!', 0);
            $condition['name'] = $name;
            $condition['pass'] = $pass;
            $r = $admin->where($condition)->find();
            $level = D('Level');
            $level_name = $level->field('name')->where('id='.$r['level'])->find();
            session('admin_name', $r['name']);
            session('admin_level', $level_name['name']);
            //更新最后ip和最后登录时间
            $condition['last_ip'] = $_SERVER['REMOTE_ADDR'];
            $condition['last_date'] = date('Y-m-d H:i:s');
            $condition['id'] = $r['id'];
            $admin->save($condition);
            $this->success('登陆成功，正在跳转到首页...', '../Index');
        }
        if(!!$r = $admin->where($condition)->find()) {

        } else {
            $this->error('用户名或者密码错误!');
        }
    }

    public function logout() {
        if(session('admin_name')) {
            session('admin_name', null);
            $this->redirect('./');
        }
    }

}


?>